package com.g2.admin.controller;

import com.g2.admin.common.constant.StatusTypeEnums;
import com.g2.admin.model.po.UserOrgPo;
import com.g2.admin.service.UserOrgService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("org")
@Api(tags = "Org")
public class OrgController extends BaseController {
    @Autowired
    private UserOrgService orgService;

    @GetMapping("/tree")
    public List<UserOrgPo> orgTree() {
        List<UserOrgPo> orgs = orgService.getOrgTree();
        return null == orgs ? Collections.EMPTY_LIST : orgs;
    }

    @PostMapping("/save")
    public void save(@RequestBody UserOrgPo rolePo) {
        if (null == rolePo.getId()) {
            orgService.addOrg(rolePo);
        } else {
            orgService.updateOrg(rolePo);
        }
    }

    @PostMapping("/enable/{id}")
    public void enable(@PathVariable("id") Long id) {
        orgService.updateOrg(UserOrgPo.builder().id(id).status(StatusTypeEnums.StatusEnums.ENABLE).build());
    }

    @PostMapping("/disable/{id}")
    public void disable(@PathVariable("id") Long id) {
        orgService.updateOrg(UserOrgPo.builder().id(id).status(StatusTypeEnums.StatusEnums.DISABLE).build());
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable("id") long roleId) {
        orgService.delOrg(roleId);
    }
}
